package org.kamionowski.supreme.services.crawler.impl;

import org.apache.commons.vfs.FileContent;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.kamionowski.supreme.persistence.entity.Service;
import org.kamionowski.supreme.services.crawler.exc.CrawlerException;

/**
 * Created by IntelliJ IDEA.
 * User: Marcin Kamionowski
 * Date: 10.04.11
 * Time: 20:13
 */
public class RemoteFile {
    private final long size;
    private final long modificationDate;
    private final String fileName;
    private final String path;
    private final Service service;

    private RemoteFile(String path, String fileName, long size, long modificationDate, Service service) {
        this.path = path;
        this.fileName = fileName;
        this.size = size;
        this.modificationDate = modificationDate;
        this.service = service;
    }

    public static RemoteFile build(FileObject file, Service service) throws CrawlerException{
        try{
            final FileName fn = file.getName();
            final FileContent fc = file.getContent();
            return new RemoteFile(
                    fn.getPath(),
                    fn.getBaseName(),
                    fc.getSize(),
                    fc.getLastModifiedTime(),
                    service
            );
        }catch(FileSystemException exc){
            throw new CrawlerException(exc, service);
        }
    }

    public long getSize() {
        return size;
    }

    public long getModificationDate() {
        return modificationDate;
    }

    public String getFileName() {
        return fileName;
    }

    public String getPath() {
        return path;
    }

    public Service getService() {
        return service;
    }

    public String getFullPath(){
        return this.getPath() + "/" + this.getFileName();
    }
}
